﻿<%@ Page Title="Idiomas" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Idiomas.aspx.cs" Inherits="RzCorportativo.Views.Administrativo.Idiomas" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LeftContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <div class="tabbable tabs-left">
        <div class="tab-content">
            <div class="tab-pane active" id="geral">
                <div class="span12 well">
                    <div class="controls controls-row">
                        <asp:TextBox runat="server" ID="Nome" placeholder="Idioma" type="text" name="complete" 
                            value="" auto-complete="false" eid="descricao" requesttype="post" contenttype="application/json; charset=utf-8"
                            isasync="true" length="1" datatype="json" templeted="true" onrender="getItemIdioma"
                            onerror="getErrorIdioma" onselected="selectedItemIdioma" data-toggle="tooltip"
                            title="Idioma" data-placement="left" has-dependent="true" target="IdiomaId"/>
                        <input type="button" name="btnNovo" value="salvar" id="btnAdionaIdioma" class="btn btn-success" />
                        <asp:HiddenField runat="server" ID="IdiomaId" />
                    </div>
                </div>
                <br />
                <div dataspy="">
                    <table class="table table-hover" id="gridIdioma" runat="server">
                        <thead>
                            <tr>
                                <th>
                                    IDIOMA
                                </th>
                                <th>
                                    #
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
                <script src="<%=this.ResolveUrl("~/Recursos/j/eComplete.js")%>" type="text/javascript"></script>
                <%--<script src="<%=this.ResolveUrl("~/Recursos/j/Idioma.js")%>" type="text/javascript"></script>--%>
                <script type="text/javascript">
                    $(function () {
                        $("input[has-dependent='true']").keyup(function () {
                            if ($(this).val().length == 0) {
                                $("input[id$='" + $(this).attr("target") + "']").val("");
                            }
                        });
                        $("input[id$='btnAdionaIdioma']").click(function (e) {
                            var idioma = new Idioma();
                            var alerta = new Alert();
                            alerta.Name = "erroGeral";
                            e.preventDefault();
                            idioma.Nome = $("input[id$='Nome']").val();
                            var id = $("input[id$='IdiomaId']").val();
                            idioma.IdiomaId = parseInt(id, 10);
                            if (id.length > 0 && isNaN(idioma.IdiomaId)) {
                                alerta.AddMessage("Código de alerta inválido");
                                idioma.IdiomaId = -1;
                            } else {
                                idioma.IdiomaId = isNaN(idioma.IdiomaId) ? 0 : idioma.IdiomaId;
                            }
                            if (idioma.Nome.length == 0) {
                                alerta.AddMessage("Código de alerta inválido");
                            }
                            if (alerta.HasMessage()) {
                                alerta.Type = AlertType.Error;
                                alerta.Show();
                                return false;
                            }
                            idioma.Salvar();
                            return false;
                        });
                    });
                    function Idioma() {
                        this.IdiomaId = 0;
                        this.Nome = "";
                        this.Salvar = function () {
                            var json = JSON.stringify(this);
                            current = this;
                            __post("Views/Administrativo/Idiomas.aspx/AddIdioma", "{ idioma :" + json + " }", function (item) {
                                if ($("tr[idioma='" + current.IdiomaId + "']").length > 0) {
                                    $("tr[idioma='" + current.IdiomaId + "']:first").remove();
                                }
                                current.IdiomaId = item.d.Value;
                                var tbody = "<tr idioma='" + current.IdiomaId.toString(10) + "'><td><a href='javascript:Editar({ Nome: \"" + current.Nome + "\", IdiomaId: " + current.IdiomaId.toString(10) + " }); void(0);'>" + current.Nome.toUpperCase() + "</a></td><td><input type='button' id='btnRemoveIdioma'" + current.IdiomaId.toString(10) +
                         " onclick='RemoveIdioma(" + current.IdiomaId.toString(10) + ", this)' class='btn btn-danger' value='remover'/></td></tr>";
                                $('table[id$="gridIdioma"] tr:last').after(tbody);
                                $("input[id$='Nome']").val("");
                                $("input[id$='IdiomaId']").val("");
                            });
                        }
                        this.Excluir = function (item) {
                            var json = JSON.stringify(this);
                            var current = this;
                            __post("Views/Administrativo/Idiomas.aspx/RemoveIdioma", "{ idioma :" + json + " }", function (item) {
                                if ($("tr[idioma='" + current.IdiomaId + "']").length > 0) {
                                    $("tr[idioma='" + current.IdiomaId + "']:first").remove();
                                }
                                current.IdiomaId = item.d.Value;
                                $("input[id$='Nome']").val("");
                                $("input[id$='IdiomaId']").val("");
                            });

                        }
                        var __post = function (url, parans, success, error) {
                            var alerta = new Alert();
                            alerta.Type = AlertType.Error;
                            alerta.Name = "erroGeral";
                            $.ajax({
                                url: eS.UrlPadrao + url,
                                type: "post",
                                dataType: "json",
                                contentType: "application/json; charset=utf-8",
                                data: parans,
                                success: function (e) {
                                    if (e.d && e.d.AlertType != undefined) {
                                        alerta.Type = e.d.AlertType;
                                        for (var i in e.d.Messages) {
                                            alerta.AddMessage(e.d.Messages[i]);
                                        }
                                        if (e.d.Show && e.d.Show == true) {
                                            alerta.Show();
                                        }
                                    } else {
                                        if (e.responseText) {
                                            alerta.Type = 2;
                                            alerta.AddMessage(e.responseText);
                                        } else {
                                            alerta.Type = 1;
                                            alerta.AddMessage("Retorno inválido, o objeto retornado não está em um formato correto")
                                        }
                                        alerta.Show();
                                    }
                                    if (alerta.Type == AlertType.Success && success && typeof success == "function") {
                                        success.call(this, e);
                                    }
                                },
                                error: function (e) {
                                    alerta.Type = 1;
                                    alerta.AddMessage(e.responseText);
                                    alerta.Show();
                                    if (error && typeof error == "function") {
                                        error.call(this, e);
                                    }
                                }
                            });
                        }
                    }

                    function getItemIdioma(ul, item) {

                        var inner_html = '<a><div class="list_item_container"><div class=""><strong>Idioma:</strong> ' +
                            item.Nome + '</div></div></a>';
                        return $("<li style='width:350px'></li>")
            .data("item.autocomplete", item)
            .append(inner_html)
            .appendTo(ul);
                    }

                    function getErrorIdioma(e) {
                        var _alert = new Alert();
                        _alert.Name = "erroGeral";
                        _alert.Type = AlertType.Error;
                        _alert.AddMessage(e.d ? e.d : e.responseText);
                        _alert.Show();
                    }
                    function selectedItemIdioma(event, ui, context) {
                        var item = ui.item && ui.item.value ? ui.item : context;
                        if (item.IdiomaId) {
                            $("input[id$='IdiomaId']").val(item.IdiomaId.toString(10))
                        }
                    }
                    function RemoveIdioma(id, link) {
                        var idioma = new Idioma();
                        idioma.IdiomaId = id;
                        idioma.Excluir(link);
                    }
                    function Editar(item) {
                        $("input[id$='IdiomaId']").val(item.IdiomaId.toString(10));
                        $("input[id$='Nome']").val(item.Nome);
                        return false;
                    }
                </script>
            </div>
        </div>
    </div>
</asp:Content>
